package com.example.bai12;

import java.util.ArrayList;

import com.example.adapter.PhongBanAdapter;
import com.example.object.PhongBan;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
	//Request code - result code
	public static int THEM_NHAN_VIEN=1;
	public static int SUA_NHAN_VIEN=2;
	public static int THEM_NV_THANH_CONG=3;
	public static int SUA_NHAN_VIEN_THANH_CONG=4;
	public static int XEM_DS_NHAN_VIEN=5;
	public static int CAP_NHAT_DS_NV_THANH_CONG=6;
	public static int THIET_LAP_TP_PP=7;
	public static int THIET_LAP_TP_PP_THANH_CONG=8;
	public static int CHUYEN_PHONG=9;
	public static int CHUYEN_PHONG_THANH_CONG=10;
	
	private Button btnluu;
	private EditText edtmapb,edttenpb;
	private ListView list;
	private PhongBanAdapter adapter;
	private PhongBan pbselected;
	private static ArrayList<PhongBan> arr= new ArrayList<PhongBan>();
    public static ArrayList<PhongBan> getArr() {
		return arr;
	}

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getControls();
        addEventForControls();
       
    }

    private void getControls()
    {
    	btnluu= (Button) findViewById(R.id.btnluu);
    	edtmapb= (EditText) findViewById(R.id.edtmapb);
    	edttenpb=(EditText) findViewById(R.id.edttenpb);
    	list= (ListView) findViewById(R.id.list);
    	adapter= new PhongBanAdapter(MainActivity.this, R.layout.layout_item_custom,arr);
    	list.setAdapter(adapter);
    }
    private void addEventForControls()
    {
    	btnluu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				savePhongBan();
			}
		});
    	list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				pbselected=arr.get(arg2);
				return false;
			}
		});
    }
    
    private void savePhongBan()
    {
    	String id=edtmapb.getText().toString();
    	String nm=edttenpb.getText().toString();
    	PhongBan p=null;
    	int i;
    	for( i=0;i<arr.size();i++)
    	{
    		 p= arr.get(i);
    		if(p.getId().equalsIgnoreCase(id))
    		{
    			break;
    		}
    	}
    	if(i<arr.size())
    	{
    		p.setName(edttenpb.getText().toString());
    		arr.set(i, p);
    	}
    	else
    	{
    		p= new PhongBan(id, nm);
    		arr.add(p);
    	}
    	adapter.notifyDataSetChanged();
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.menuphongban, menu);
	}
	

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId())
		{
			case R.id.itemthemnv:
				callThemNhanVien();
				break;
			case R.id.itemthietlaptp:
				callThietLapLanhDao();
				break;
			case R.id.itemxemdsnv:
				callXemDanhSachNhanVien();
				break;
			case R.id.itemxoaphong:
				xoaPhongBan();
				break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
    
	private void callThemNhanVien()
	{
		Intent i= new Intent(MainActivity.this,ThemNhanVienActivity.class);
		startActivityForResult(i,THEM_NHAN_VIEN);
	}
	
	private void callXemDanhSachNhanVien()
	{
		Intent i= new Intent(MainActivity.this, XemDanhSachNhanVienActivity.class);
		Bundle b= new Bundle();
		b.putSerializable("PHONGBAN", pbselected);
		i.putExtra("DATA",b);
		startActivityForResult(i, XEM_DS_NHAN_VIEN);
		
	}
	private void callThietLapLanhDao()
	{
		Intent i = new Intent(MainActivity.this,ThietLapLanhDaoActivity.class);
		Bundle b= new Bundle();
		b.putSerializable("PHONGBAN", pbselected);
		i.putExtra("DATA",b);
		startActivityForResult(i,THIET_LAP_TP_PP);
	}
	private void xoaPhongBan()
	{
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("Hỏi");
		builder.setMessage("Bạn thật sự muốn xóa phòng ban '"+pbselected.getName()+"' ?");
		builder.setIcon(android.R.drawable.ic_delete);
		builder.setNegativeButton("Không", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		builder.setPositiveButton("Có", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				arr.remove(pbselected);
				adapter.notifyDataSetChanged();
			}
		});
		builder.show();
	}
    
}
